package DataStructure;

import org.joml.Vector3f;

import java.awt.*;
import java.awt.image.BufferedImage;

public class Face {
    private int[] vertexIndices;                //顶点索引
    private int[] normalIndices;                //法线索引
    private int[] textureCoordinateIndices;     //纹理坐标索引
    public Model.Material material = null;
    public Color faceColor = null;

    //构造函数
    public Face() {}
    //有参构造函数：带参数的构造方法，用于在创建Face对象时初始化顶点索引、法线索引和纹理坐标索引
    public Face(int[] vertexIndices,
                int[] normalIndices,
                int[] textureCoordinateIndices) {
        this.vertexIndices = vertexIndices;
        this.normalIndices = normalIndices;
        this.textureCoordinateIndices = textureCoordinateIndices;
    }


    //getter方法
    public int[] getVertexIndices()             {return vertexIndices;}
    public int[] getNormalIndices()             {return normalIndices;}
    public int[] getTextureCoordinateIndices()  {return textureCoordinateIndices;}
    //setter方法(add):创建内容或者添加内容
    public void addVertexIndices(int index) {
        if (vertexIndices == null) {
            vertexIndices = new int[3];
        }
        for (int i = 0; i < vertexIndices.length; i++) {
            if (vertexIndices[i] == 0) {
                vertexIndices[i] = index;
                break;
            }
        }
    }
    public void addNormalIndices(int index) {
        if (normalIndices == null) {
            normalIndices = new int[3];
        }
        for (int i = 0; i < normalIndices.length; i++) {
            if (normalIndices[i] == 0) {
                normalIndices[i] = index;
                break;
            }
        }
    }
    public void addTextureCoordinateIndices(int index) {
        if (textureCoordinateIndices == null) {
            textureCoordinateIndices = new int[3];
        }
        for (int i = 0; i < textureCoordinateIndices.length; i++) {
            if (textureCoordinateIndices[i] == 0) {
                textureCoordinateIndices[i] = index;
                break;
            }
        }
    }
    public Color getColorofFace(Model model){
        if(material.path == null){
            // 将浮点数颜色分量转换为0-255的整数
            int red = (int) (material.diffuseColor.x * 255);
            int green = (int) (material.diffuseColor.y * 255);
            int blue = (int) (material.diffuseColor.z * 255);
            Color c =new Color(
                    Math.max(0, Math.min(red, 255)),
                    Math.max(0, Math.min(green, 255)),
                    Math.max(0, Math.min(blue, 255))
            );

            return c;
        }
        BufferedImage image = material.textureImage;
        TextureCoordinate[] t = new TextureCoordinate[3];
        Color[] c = new Color[3];
        for (int i = 0; i < 3; i++) {
            t[i] = model.getTextureCoordinates().get(textureCoordinateIndices[i]);
        }
        for (int i = 0; i < 3; i++) {
            float u = t[i].getU();
            float v = t[i].getV();

            // 计算纹理坐标并限制在图像范围内
            int x = ((int) (u * (image.getWidth() - 1)));
            x = Math.max(0, Math.min(x, image.getWidth() - 1));
            int y = ((int) ((1.0f-v) * (image.getHeight() - 1)));
            y = Math.max(0, Math.min(y, image.getHeight() - 1));

            c[i] = new Color(image.getRGB(x, y));
        }
        float redSum = 0, greenSum = 0, blueSum = 0;
        for (int i = 0; i < 3; i++) {
            redSum += c[i].getRed();
            greenSum += c[i].getGreen();
            blueSum += c[i].getBlue();
        }

        int avgRed = Math.round(redSum / 3);
        int avgGreen = Math.round(greenSum / 3);
        int avgBlue = Math.round(blueSum / 3);

        faceColor = new Color(avgRed, avgGreen, avgBlue);

        return faceColor;
    }
    public Vector3f calculateFaceNormal(Model model) {
        // 确保面是三角形且顶点存在
        if (vertexIndices == null || vertexIndices.length != 3) {
            throw new IllegalStateException("Face does not have exactly 3 vertices");
        }

        // 从Model中获取顶点坐标（假设索引从0开始）
        Vector3f v0 = model.getVertices().get(vertexIndices[0]);
        Vector3f v1 = model.getVertices().get(vertexIndices[1]);
        Vector3f v2 = model.getVertices().get(vertexIndices[2]);

        // 计算两个边向量
        Vector3f edge1 = new Vector3f(v1.x - v0.x, v1.y - v0.y, v1.z - v0.z);
        Vector3f edge2 = new Vector3f(v2.x - v0.x, v2.y - v0.y, v2.z - v0.z);

        // 叉乘计算法线
        Vector3f normal = edge1.cross(edge2);

        // 归一化法线向量
        normal.normalize();

        return normal;
    }
}